/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

import constants.UiConstants;
import exceptions.NoTnsAdminFileException;
import ui.messages.MessageBoxes;
import ui.windows.forms.serverForm.ServerForm;
import utils.properties.Configuration;
import utils.tns.GetTNS;

import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;


/**
 * Main class - this is where everything is initialized and program is started.
 *
 * @author rymek
 *         User: rymek
 *         Date: 30.06.11
 *         Time: 10:10
 */
class RymekMessageBrowser {

    public static void main(String[] args) {

        // initialize the app
        RymekMessageBrowser.initialize();
    }

    /**
     * Method initializes all required global settings
     * and does some other magic tricks
     */
    private static void initialize() {

        try {

            // prints app version
            System.out.println(UiConstants.VERSION.replace("%1", UiConstants.VERSION_NUMBER));

            // loads general properties
            loadGeneralSettings();

            // Set look & feel to System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            // Register oracle driver
            java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

            // initializes TNS system
            initializeTNS();

            // create main window
            new ServerForm().createServerForm();

        } catch (SQLException sqlException) {
            MessageBoxes.sqlException(sqlException);

        } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException exception) {
            MessageBoxes.genericErrorMessage(exception);
        }
    }

    /**
     * Initializes TNS system and handles errors if tnsnames.ora
     * file is not found
     */
    private static void initializeTNS() {

        try {

            if (Configuration.generalProperties.getOraclePath() != null) {
                GetTNS.setOraclePath(Configuration.generalProperties.getOraclePath());
            }

            GetTNS.getTns();
            Configuration.generalProperties.exportSettings();

        } catch (FileNotFoundException exception) {

            try {

                MessageBoxes.genericErrorMessage(exception);
                throw new NoTnsAdminFileException();

            } catch (NoTnsAdminFileException noTnsAdminFileException) {
                MessageBoxes.noTnsAdminException(noTnsAdminFileException);
                GetTNS.setOraclePath(MessageBoxes.chooseOracleInstallDir());
                Configuration.generalProperties.setOraclePath(GetTNS.getOraclePath());
                initializeTNS();
            }

        } catch (NoTnsAdminFileException noTnsAdminFileException) {
            MessageBoxes.noTnsAdminException(noTnsAdminFileException);
            GetTNS.setOraclePath(MessageBoxes.chooseOracleInstallDir());
            Configuration.generalProperties.setOraclePath(GetTNS.getOraclePath());
            initializeTNS();

        } catch (IOException exception) {
            MessageBoxes.genericErrorMessage(exception);
        }
    }

    /**
     * Loads whole configuration from file
     */
    private static void loadGeneralSettings() {

        try {
            new Configuration();

        } catch (IOException e) {
            MessageBoxes.settingsLoadError();
        }
    }
}